<template>
  <BaseDialog
    v-model="dialogVisible"
    :title="title"
    width="650px"
    :before-close="handleClose"
  >
    <el-form
      ref="refDialogForm"
      v-loading="formLoading"
      class="set-dialog-form"
      :label-width="language === 'en' ? '180px' : '80px'"
      :model="formData"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="工艺名称" prop="name">
            <TextInput v-model="formData.name" label="工艺名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="制作方法" prop="processMethod">
            <TextInput v-model="formData.processMethod" label="制作方法" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="说明" prop="description">
            <TextInput v-model="formData.description" label="说明" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="form-dialog-footer">
        <el-button plain @click="handleClose">{{
          $t('public.cancel')
        }}</el-button>
        <el-button type="primary" @click="handleSubmitForm">{{
          $t('public.confirm')
        }}</el-button>
      </div>
    </template>
  </BaseDialog>
</template>

<script setup>
const props = defineProps({
  type: {
    type: String,
    default: 'add' // add | edit
  },
  id: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update'])
const dialogVisible = defineModel({
  type: Boolean,
  default: false
})
const { language } = storeToRefs(useAppStore())
const title = computed(() => (props.type === 'add' ? '新建工艺' : '编辑工艺'))

const refDialogForm = ref(null)
const formLoading = ref(false)
const formData = ref({
  name: '',
  processMethod: '',
  description: ''
})
const rules = {
  name: g_rules.commonInputText('工艺名称')
  // processMethod: g_rules.commonInputText('制作方法'),
  // description: g_rules.commonInputText('说明')
}

onMounted(() => {
  props.type === 'edit' && getFormData()
})

const getFormData = _ => {
  g_api.foodstuff
    .apiProcessQueryDetail({ id: props.id })
    .then(({ code, data }) => {
      if (code === '200' && data) {
        formData.value = {
          ...formData.value,
          id: ''
        }
        g_utils.assignFormData(formData.value, data)
        console.log('formData.value', formData.value)
      }
    })
}

const handleClose = _ => {
  dialogVisible.value = false
}

const handleSubmitForm = () => {
  if (formLoading.value) return
  refDialogForm.value.validate(valid => {
    console.log('formData.value', formData.value)
    if (!valid) return
    formLoading.value = true
    const urlMethod =
      props.type === 'add'
        ? g_api.foodstuff.apiProcessCreate
        : g_api.foodstuff.apiProcessUpdate
    urlMethod({
      ...formData.value
    })
      .then(res => {
        if (res.code === '200') {
          ElMessage({
            message: res.message,
            type: 'success'
          })
          handleClose()
          emit('update')
        }
      })
      .finally(() => {
        formLoading.value = false
      })
  })
}
</script>

<style scoped lang="scss"></style>
